package com.ynny.chapter06;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {


        String userName = request.getParameter("userName");

        String password = request.getParameter("pwd");

        //从数据库 查询用户名密码 是否正确

        //模拟用户名 不存在
        if (!userName.equals("admin")){
            request.setAttribute("msg","用户名不存在");
            request.getRequestDispatcher("/login.jsp")
                    .forward(request,response);
        }


        if (userName.equals("admin") && password.equals("123456")) {
            //登录成功
            //重定向 跳转
            response.sendRedirect("/chapter03/index.jsp");

        }else {
            //登录失败

            request.setAttribute("msg","用户名或密码错误");

            request.getRequestDispatcher("/login.jsp")
                    .forward(request,response);
//            response.sendRedirect("/chapter03/login.jsp");
        }


    }
}
